package com.zijie.shoprbacapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.rbac.model.Resource;
import com.zijie.rbac.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourceApi {
    private ResourceService resourceService;

    //依赖注入：
    @Autowired
    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    //查询全部：
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") Integer pageNo,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 Resource resource) {
        Page<Resource> page = new Page<>(pageNo, pageSize);
        Page<Resource> all = resourceService.findAll(page, resource);
        return ResponseEntity.ok(JsonResult.success(all));
    }

    //新增：
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> save(@RequestBody Resource resource) {
        return ResponseEntity.ok(JsonResult.success(resourceService.add(resource)));
    }

    //修改：
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Resource resource) {
        return ResponseEntity.ok(JsonResult.success(resourceService.update(resource)));
    }

    //删除：
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(resourceService.deleteById(id)));
    }


    //批量删除：
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> deleteAll(@RequestParam("ids") List<Integer> ids) {
        return ResponseEntity.ok(JsonResult.success(resourceService.deleteByIds(ids)));
    }


    //根据id查询：
    @RequestMapping("/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        return ResponseEntity.ok(JsonResult.success(resourceService.findById(id)));
    }
}
